package com.zzh.takeoutapp.adapter;


import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.entity.FoodBean;
import com.zzh.takeoutapp.event.FoobEvent;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.view.goods.AddWidget;

import java.util.List;

public class FoodAdapter extends BaseQuickAdapter<FoodBean, BaseViewHolder> {
    public static final int FIRST_STICKY_VIEW = 1;
    public static final int HAS_STICKY_VIEW = 2;
    public static final int NONE_STICKY_VIEW = 3;
    private List<FoodBean> flist;
    private AddWidget.OnAddClick onAddClick;

    public FoodAdapter(@Nullable List<FoodBean> data, AddWidget.OnAddClick onAddClick) {
        super(R.layout.item_food, data);
        flist = data;
        this.onAddClick = onAddClick;
    }


    @Override
    protected void convert(BaseViewHolder helper, final FoodBean item) {

        helper.setText(R.id.tv_name, item.getName())
                .setText(R.id.tv_summary, item.getIntro())
                .setText(R.id.tv_price, item.getStrPrice(mContext))
                .setText(R.id.tv_sale, item.getSale())
                .addOnClickListener(R.id.addwidget)
                .addOnClickListener(R.id.text_is_spec)
                .addOnClickListener(R.id.food_main);
        ImageManager.loadUrlImage(mContext, item.getPhoto(), (ImageView) helper.getView(R.id.iv_food));
        AddWidget addWidget = helper.getView(R.id.addwidget);
        addWidget.setData(this, onAddClick, flist, item);
        if (item.getSpecsX().size() > 0 || item.getSpecification().size() > 0) {
            helper.setVisible(R.id.is_spec, true);
            helper.setVisible(R.id.addwidget, false);
        } else {
            helper.setVisible(R.id.addwidget, true);
            helper.setVisible(R.id.is_spec, false);
        }
        if (item.getSelectCount() > 0) {
            helper.setVisible(R.id.item_badge, true).setText(R.id.item_badge,
                    String.valueOf(item.getSelectCount()));
        } else {
            helper.setVisible(R.id.item_badge, false);
        }
        if (helper.getAdapterPosition() == 0) {
            helper.setVisible(R.id.stick_header, true)
                    .setText(R.id.tv_header, item.getType())
                    .setTag(R.id.food_main, FIRST_STICKY_VIEW);
        } else {
            if (!TextUtils.equals(item.getType(), flist.get(helper.getAdapterPosition() - 1).getType())) {
                helper.setVisible(R.id.stick_header, true)
                        .setText(R.id.tv_header, item.getType())
                        .setTag(R.id.food_main, HAS_STICKY_VIEW);
            } else {
                helper.setVisible(R.id.stick_header, false)
                        .setTag(R.id.food_main, NONE_STICKY_VIEW);
            }
        }
        helper.getConvertView().setContentDescription(item.getType());
    }

}
